package com.stoneframe.base.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.stoneframe.base.dao.DaoSupport;
import com.stoneframe.base.util.PageData;
import com.stoneframe.base.util.Pagination;

public class BaseService {
	
	protected String mapper="";
	
	@Resource(name="daoSupport")
	protected DaoSupport dao;
	
	protected BaseService(String mapper){
		this.mapper=mapper;
	}
	
	/***
	 * 获取全部
	 * */
	public List<PageData> listAll(PageData pd) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".listAll", pd);
	}
	
	/***
	 * 分页获取
	 * */
	public List<PageData> listPage(Pagination page) throws Exception{
		List<PageData> rows=(List<PageData>)dao.findForList(mapper+".listPage", page);
		int records=rows.size();
		if(page.getIsGetRecord()){
			records=Integer.parseInt(dao.findForObject(mapper+".findCount", page).toString());
		}else{
			records=(records==page.getRows()?records+1:records);
		}
		page.setRecords(records);
		return rows;
	}
	
	/**
	 * 获取总记录数
	 */
	public int findCount(PageData pd) throws Exception{
		return Integer.parseInt(dao.findForObject(mapper+".findCount", pd).toString());
	}
	
	/**
	 * 是否存在
	 */
	public Boolean isExist(PageData pd) throws Exception{
		return Integer.parseInt(dao.findForObject(mapper+".isExist", pd).toString())>0?true:false;
	}
	
	/**
	 * 查找对象
	 */
	public PageData findById(PageData pd) throws Exception{
		return (PageData)dao.findForObject(mapper+".findById", pd);
	}
	
	/**
	 * 保存对象
	 */
	public void save(PageData pd) throws Exception {
		dao.save(mapper+".save", pd);
	}
	
	/**
	 * 批量保存
	 */
	public void save(List list) throws Exception{
		dao.save(mapper+".saveBatch", list);
	}
	
	/**
	 * 修改对象
	 */
	public void edit(PageData pd) throws Exception {
		dao.edit(mapper+".edit", pd);
	}
	
	/**
	 * 批量修改
	 */
	public void edit(List list) throws Exception{
		dao.edit(mapper+".editBatch",list);
	}
	
	/**
	 * 删除对象 
	 */
	public void del(PageData pd) throws Exception {
		dao.edit(mapper+".del", pd);
	}
	
	/**
	 * 批量删除
	 */
	public void del(List list) throws Exception{
		dao.edit(mapper+".delBatch", list);
	}

}
